package demo4.controller;

import demo4.mapper.IUserMapper;
import demo4.mapper.UserMapper;
import demo4.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.awt.*;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import java.util.List;

@Component
public class UserController {
    @Autowired
    private IUserMapper userMapper;
    public void start(){
        System.out.println("请输入操作序号:");
        Scanner scanner = new Scanner(System.in);
        System.out.println("1.注册");
        System.out.println("2.登录");
        System.out.println("3.修改密码");
        System.out.println("4.用户列表");
        int num = scanner.nextInt();
        switch (num){
            case 1:
                regUser();
                break;
            case 2:
                loginUser();
                break;
            case 3:
                updateUser();
                break;
            case 4:
                listUser();
                break;
                default:
                    System.out.println("输入错误");
        }
    }

    private void loginUser(){
//        System.out.println("欢迎登陆！");
//        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入用户名:");
//        String username = scanner.nextLine();
//        System.out.println("请输入密码:");
//        String password = scanner.nextLine();
//
//        User user = new User(0,username,password,"",0);
//
//        System.out.println(userMapper.selectUser( user));
        System.out.println("欢迎登陆！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();

        User user = userMapper.selectUserByUsername(username);
        if(user != null){
            if (user.getPassword().equals( password)) {
                System.out.println("登录成功，欢迎【" + user.getNickname() + "】回来");
            } else {
                System.out.println("登录失败，密码错误");
            }
        }

    }

    private void regUser(){
        System.out.println("欢迎注册！");
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入密码:");
        String password = scanner.nextLine();
        System.out.println("请输入昵称:");
        String nickname = scanner.nextLine();
        System.out.println("请输入年龄:");
        int age = scanner.nextInt();

        if(userMapper.selectUserByUsername( username)!= null){
            System.out.println("用户名已存在");
            return;
        }

        User user = new User(0,username,password,nickname,age);

        int result = userMapper.insertUser(user);
        System.out.println(result > 0 ? "注册成功" : "注册失败");
    }

    private void listUser(){
       List<User> list = userMapper.selectAllUser();
       for(User user : list){
           System.out.println(user);
       }
    }

    private void updateUser(){
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名:");
        String username = scanner.nextLine();
        System.out.println("请输入旧密码:");
        String oldPassword = scanner.nextLine();
        System.out.println("请输入新密码:");
        String newPassword = scanner.nextLine();

        User user = userMapper.selectUserByUsername(username);

        if(user !=null){
            if(user.getPassword().equals(oldPassword)){
                user.setPassword(newPassword);
                int result = userMapper.updateUser(user);
                System.out.println( result > 0 ? "修改成功" : "修改失败");
            }else {
                System.out.println("旧密码输入错误");
            }
        }else {
            System.out.println("用户不存在");
        }


    }
}
